var shopId = '';
var baseUrl = 'http://192.168.0.151:8083'; // 服务器地址
var pirUrl = 'http://61.144.241.236:10089/'; // 图片路径
var focusState = true;
var pushEvaluation = false;  // 用户评价 push/add
var footClick = true;  // 是否查看更多
var pages = {
  pageNo:1,
  pageSize:10,
  evalStatus:0
}
$(document).ready(function(){   
  shopId = '0600400007120190215';
  getHotCategory();
  getShopDetail(shopId);
  getShopService(shopId);
  getEvaluationTotal(shopId);
  getEvaluation(shopId);
})

// 获取热门类目
function getHotCategory(){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/homeCategoryController/findHomeCategory",
    type:"get",	
    data:"",	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        var str = '';
        for(var i = 0 ; i < res.data.length ; i ++){
          str += '<a href="#">'+res.data[i].cateName+'</a>'
        }
        $('#hot-category').append(str);
      }
    },
    error:function(res){

    },
  })
};

// 关注/取消关注
function clickFocus(){
  var api = focusState?'/addCollectShop':'/removeCollectShops';
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/collectShopRecord"+api,
    type:"get",	
    data:"shopId="+shopId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        $('.caozuo').children().eq(0).remove();
        if(focusState){
          $('.caozuo').prepend('<span class="focus-click" onclick="clickFocus()"><i class="fa fa-heart"></i>关注</span></span>');
        }else{
          $('.caozuo').prepend('<span class="focus-click" onclick="clickFocus()"><i class="fa fa-heart-o"></i>关注</span></span>');
        }
        focusState = !focusState;
      }
    },
    error:function(res){

    },
  })
};
// 查看更多评价
function addEvaluation(){
  pages.pageNo += 1;
  pushEvaluation = true;
  getEvaluation(shopId);
};

// 评价分类选择
function getUserEvaluation(state){
  if(pages.evalStatus != state){
    $('.more').text('查看更多评价');
    $('.more').append('<i class="fa fa-caret-down"></i>');
    pushEvaluation = false;
    footClick = true;
    pages.pageNo = 1;
    pages.evalStatus = state;
    $('.com-category span:eq(' + state + ')').css({'background': 'rgb(35,106,173)','color': '#fff'})
    .siblings().css({'background': '#fff','color': 'rgb(16,16,16)'})
    $('.comments .tabChange ul:eq(' + state + ')').css({'display': 'block'})
    .siblings().css({'display': 'none'});
    getEvaluation(shopId);
  }
};

// 获取店铺信息
function getShopDetail(id){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/shop/findShopBasicInfo",
    type:"get",	
    data:"shopId="+id,	
    dataType:"json", 
    success:function(res){
      // console.log(res)
      if(res.status === 1){
        var data = res.data;
        focusState = res.data.collectStatus==0?true:false;
        console.log(focusState)
        console.log(data)
        $('.name').append('<h4><a href="shopsDetail.html">'+data.entName+'</a>'
        +'<img src="'+data.shopLogo+'"/>'+
        '<i class="fa fa-comments"></i> </h4>');
        var focus = data.collectStatus==1?'<i class="fa fa-heart"></i>关注</span>':'<span class="focus-click"><i class="fa fa-heart-o"></i>关注';
        $('.caozuo').prepend('<span class="focus-click" onclick="clickFocus()">'+focus+'</span>');
        $('#store-num').prepend('<p>('+data.entResno.substring(0,2)+' '+
        data.entResno.substring(2,5)+' '+data.entResno.substring(5,7)+' '+
        data.entResno.substring(7,11)+')</p>');
        var ShopScore = Math.round(data.shopEvaluateValue);
        var ShopStar = '';
        for(var i = 0 ; i < ShopScore ; i ++){
          ShopStar += '<i class="fa fa-star"></i>';
        }
        if(ShopScore != 5){
          var cha = 5 - ShopScore;
          for(var j = 0 ; j < cha ; j ++){
            ShopStar += '<i class="fa fa-star-o"></i>';
          }
        }
        $('.com-point').prepend('<h4>'+data.shopEvaluateValue+'</h4>');
        $('.point-det p').append(ShopStar);
        $('.pingfen').append('<div>'+ShopStar+'</div>'+
        '<span>'+data.shopEvaluateValue+'</span>'+'<span>'+data.areaName+'</span>');
        $('.three').prepend('<div class="three-address">地址：'+data.shopAddress+
        '<i class="fa fa-map-marker"></i>'+'<a href="#">[查看地图]</a>'+'</div>');
        var weekTime = '';
        if(data.serviceTimeType == 0){
          weekTime += '<p><span>'+data.serviceRangeTime.split(',')[0]+'至'+data.serviceRangeTime.split(',')[1]+'</span>';
        }else{
          weekTime = '<p>';
          for(var i = 0 ; i < data.serviceTime.length ; i ++){
            weekTime += '<span>周'+data.serviceTime[i].startDate+'至周'+data.serviceTime[i].endDate+' : '+
            data.serviceTime[i].starTime+'-'+data.serviceTime[i].endTime+'<br />'
          }
          weekTime += '</p>'
        }
        $('#open-hours').append(weekTime);
        $('#shop-phone').append('<p>'+data.shopPhone+'</p>');
        $('.imgBox').append('<img src="'+data.picList[0]+'" />')
        var map = new BMap.Map("map"); // 创建地图实例  
				var point = new BMap.Point(data.shopLandmark.split(',')[1], data.shopLandmark.split(',')[0]); // 创建点坐标  
				map.centerAndZoom(point, 15); // 初始化地图，设置中心点坐标和地图级别
				map.addControl(new BMap.ScaleControl()); //地图控件比例尺
				map.addControl(new BMap.OverviewMapControl()); //地图控件
      }
    },
    error:function(res){
      console.log(res)
    }
  });
};

// 获取店铺服务信息
function getShopService(id){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/goodsBasicInfo/findGoodsInfoByShopId",
    type:"get",	
    data:"shopId="+id,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status == 1){
        var serviceList ='';
        for(var i = 0 ; i < res.data.length ; i ++){
          serviceList += '<li><img src="'+pirUrl+res.data[i].goodsImg+'" />'+
          '<div><h4>'+res.data[i].goodsName+'</h4><a href="#">'+
          res.data[i].categoryPName+'、'+res.data[i].categorySubName+'</a></div></li>'
        }
        $('.service-ul').append(serviceList);
      }
    },
    error:function(res){
      console.log(res)
    }
  })
};

// 获取用户评价数量
function getEvaluationTotal(id){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/evaluation/initEvaluationByShopId",
    type:"get",	
    data:"shopId="+id,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        $('.com-category').append('<span onclick="getUserEvaluation(0)">全部('+res.data.totalEval+')</span>'+
        '<span onclick="getUserEvaluation(1)">好评('+res.data.wellEval+')</span>'+
        '<span onclick="getUserEvaluation(2)">差评('+res.data.badEval+')</span>'+
        '<span onclick="getUserEvaluation(3)">有图('+res.data.hasPic+')</span>');
        $('.rate').append('<span>好评率'+res.data.wellPoint+'%</span>');
        $('.com-category span:eq(0)').css({'background': 'rgb(35,106,173)','color': '#fff'})
      }
    },
    error:function(res){

    }
  })
};

// 获取用户评价
function getEvaluation(id){
  if(footClick){
    $.ajax({
      url: baseUrl+"/hsent-swfw-user/evaluation/findEvaluationByShopId",
      type:"get",	
      data:"shopId="+id+
      '&pageSize='+pages.pageSize+
      '&pageNo='+pages.pageNo+
      '&evalStatus='+pages.evalStatus,	
      dataType:"json", 
      success:function(res){
        console.log(res);
        if(res.status === 1){
          if(res.data.evalList.length==0){
            footClick = false;
            $('.more').text('暂无更多数据');
            console.log(footClick);
            console.log($('.more'))
          }
          if(pages.evalStatus == 0){
            appendHTML($('#all-evaluation'),res);
          };
          if(pages.evalStatus == 1){
            appendHTML($('#good-evaluation'),res);
          };
          if(pages.evalStatus == 2){
            appendHTML($('#bad-evaluation'),res);
          };
          if(pages.evalStatus == 3){
            appendHTML($('#img-evaluation'),res);
          };
        }
      },
      error:function(res){
  
      }
    })
  }
};

// 用户评价 html
function appendHTML(dom,res){
  if(!pushEvaluation){
    dom.children().remove();
  };
  for(var i = 0 ; i < res.data.evalList.length ; i ++){
    var listStr = '<li><div class="left"><img src="'+pirUrl+res.data.evalList[i].userHeadUrl+'" /></div>'+
    '<div class="center"><p>'+res.data.evalList[i].userName+'</p><p>';
    var evaluateValue = Math.round(res.data.evalList[i].evaluateValue);
    for(var l = 0 ; l < evaluateValue ; l ++){
      listStr += '<i class="fa fa-star"></i>';
    }
    if(evaluateValue != 5){
      var cha = 5 - evaluateValue;
      for(var j = 0 ; j < cha ; j ++){
        listStr += '<i class="fa fa-star-o"></i>';
      }
    }
    listStr += '</p>'+res.data.evalList[i].evaluateContent+'</p><div>';
    if(res.data.evalList[i].imgList != null){
      for(var k = 0 ; k < res.data.evalList[i].imgList.length ; k ++){
        listStr += '<img src="'+pirUrl+res.data.evalList[i].imgList[k]+'" />'
      }
    }
    var newStr = res.data.evalList[i].reply?res.data.evalList[i].reply:'';
    listStr += '</div><div class="xiangguan"><img src="'+pirUrl+res.data.evalList[i].goodsImg+'" alt="" />'+
    '<div><h4>'+res.data.evalList[i].goodsName+'</h4><p>'+res.data.evalList[i].goodsAdvantage+'</p></div></div><div class="huifu">'+
    '<strong>商家回复：</strong><span>'+newStr+'</span></div></div><div class="right">'+
    res.data.evalList[i].evaluateTime+'</div></li>';
    dom.append(listStr);
  }
};
